package com.llkj.qingnianguoxue.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.customview.BlurTransformation;
import com.xiaweizi.cornerslibrary.CornersProperty;
import com.xiaweizi.cornerslibrary.RoundCornersTransformation;

import java.io.IOException;

/**
 * glide图片加载
 * Created by zhu.px on 2016/12/13.
 */
public class ImageLoadingUtil {

    public static void loadingImg(ImageView v, String url, int type) {
        int defaultIcon = 0;
        switch (type) {
            case 0:
                defaultIcon = R.drawable.icon_login_user;
                break;
            case 1:
                break;
            default:

                break;
        }
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(defaultIcon)
                .placeholder(defaultIcon)
                .transform(new CenterCrop(v.getContext())
                        , new GlideCircleTransform(v.getContext()))
                .crossFade()
                .into(v);
    }

    public static void loadingUrl(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(defaultIcon)
//                .placeholder(defaultIcon)
                .crossFade()
                .into(v);
    }

    public static void loadingUrlCenterCrop(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(defaultIcon)
//                .placeholder(defaultIcon)
                .centerCrop()
                .crossFade()
                .into(v);
    }

    //毛玻璃、高斯
    public static void loadBlurTransImg(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .bitmapTransform(new BlurTransformation(v.getContext(), 50))
//                .error(DEFAULT_PIC)
                .into(v);
    }

    /**
     * @param v
     * @param v
     * @param url
     */
    public static void loadImgGlideRound(ImageView v, String url, float corners, CornersProperty.CornerType type) {
        CornersProperty cornersProperty = new CornersProperty();
        cornersProperty.setCornersRadius(DisplayUtil.dip2px(v.getContext(), corners));
        cornersProperty.setCornersType(type);
        RoundCornersTransformation transformation =
                new RoundCornersTransformation(v.getContext(), cornersProperty);
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .bitmapTransform(new CenterCrop(v.getContext())
                        , transformation)
                .into(v);
    }


    /**
     * @param v
     * @param v
     * @param url
     */
    public static void loadImgGlideRound(ImageView v, String url, float corners) {
        CornersProperty cornersProperty = new CornersProperty();
        cornersProperty.setCornersRadius(DisplayUtil.dip2px(v.getContext(), corners));
        RoundCornersTransformation transformation =
                new RoundCornersTransformation(v.getContext(), cornersProperty);
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .bitmapTransform(transformation)
                .into(v);
    }

    /**
     * @param v
     * @param v
     * @param url
     */
    public static void loadImgGlideRound(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .crossFade()
                .transform(new GlideRoundTransform(v.getContext()))
                .into(v);
    }

    public static void loadImgGlideRoundCenterCrop(ImageView v, String url) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .crossFade()
                .transform(new CenterCrop(v.getContext())
                        , new GlideRoundTransform(v.getContext()))
                .into(v);
    }


    /**
     * 带白色边框的圆形图片加载
     *
     * @param v
     * @param url
     * @param color
     */
    public static void loadingCircleBorderImg(ImageView v, String url, int color) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.icon_login_user)
                .placeholder(R.drawable.icon_login_user)
                .transform(new GlideCircleBorderTransform(v.getContext(), 2, v.getContext().getResources().getColor(color)))
                .crossFade()
                .into(v);
    }

    /**
     * 带白色边框的圆形图片加载
     *
     * @param v
     * @param url
     * @param color
     */
    public static void loadingCircleBorderImgCenterCrop(ImageView v, String url, int color) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.icon_login_logo)
//                .placeholder(R.drawable.icon_login_logo)
                .transform(new CenterCrop(v.getContext())
                        , new GlideCircleBorderTransform(v.getContext(), 2, v.getContext().getResources().getColor(color)))
                .crossFade()
                .into(v);
    }

    /**
     * 带白色边框的圆形图片加载
     *
     * @param v
     * @param url
     * @param color
     */
    public static void loadingCircleBorderImg(ImageView v, String url, int color, int border) {
        Glide.with(v.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.default_pino)
                .placeholder(R.drawable.default_pino)
                .transform(new GlideCircleBorderTransform(v.getContext(), border, v.getContext().getResources().getColor(color)))
                .crossFade()
                .into(v);
    }

    /**
     * 带白色边框的圆形图片加载
     *
     * @param v
     * @param res
     * @param color
     */
    public static void loadingCircleBorderImg(ImageView v, int color, int res) {
        Glide.with(v.getContext())
                .load(res)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.icon_login_logo)
//                .placeholder(R.drawable.icon_login_logo)
                .transform(new GlideCircleBorderTransform(v.getContext(), 2, v.getContext().getResources().getColor(color)))
                .crossFade()
                .into(v);
    }

    public static void loadLocalImg(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .crossFade()
                .into(v);
    }

    public static void loadLocalImgCenterCrop(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .centerCrop()
                .crossFade()
                .into(v);
    }

    public static void loadImg(ImageView v, int id) {
        Glide.with(v.getContext())
                .load(id)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .transform(new GlideCircleTransform(v.getContext()))
                .crossFade()
                .into(v);
    }

    /**
     * @param v
     * @param id
     * @param type 1圆形，2圆角，其他正常
     */
    public static void loadImgType(ImageView v, int id, int type) {
        DrawableTypeRequest request = Glide.with(v.getContext()).load(id);
        request.diskCacheStrategy(DiskCacheStrategy.ALL)
//                .error(R.drawable.ease_default_avatar)
//                .placeholder(R.drawable.ease_default_avatar)
                .crossFade()
                .into(v);
        if (type == 1) {
            request.transform(new GlideCircleTransform(v.getContext()));
        } else if (type == 2) {
            request.transform(new GlideRoundTransform(v.getContext(), 3));
        }
    }

    public static Bitmap decodeScaleImage(String var0, int var1, int var2) {
        BitmapFactory.Options var3 = getBitmapOptions(var0);
        int var4 = calculateInSampleSize(var3, var1, var2);
        Log.d("img", "original wid" + var3.outWidth + " original height:" + var3.outHeight + " sample:" + var4);
        var3.inSampleSize = var4;
        var3.inJustDecodeBounds = false;
        Bitmap var5 = BitmapFactory.decodeFile(var0, var3);
        int var6 = readPictureDegree(var0);
        Bitmap var7 = null;
        if (var5 != null && var6 != 0) {
            var7 = rotateImageView(var6, var5);
            var5.recycle();
            var5 = null;
            return var7;
        } else {
            return var5;
        }
    }

    public static int readPictureDegree(String var0) {
        short var1 = 0;

        try {
            ExifInterface var2 = new ExifInterface(var0);
            int var3 = var2.getAttributeInt("Orientation", 1);
            switch (var3) {
                case 3:
                    var1 = 180;
                    break;
                case 6:
                    var1 = 90;
                    break;
                case 8:
                    var1 = 270;
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return var1;
    }

    public static Bitmap rotateImageView(int var0, Bitmap var1) {
        Matrix var2 = new Matrix();
        var2.postRotate((float) var0);
        Bitmap var3 = Bitmap.createBitmap(var1, 0, 0, var1.getWidth(), var1.getHeight(), var2, true);
        return var3;
    }

    public static BitmapFactory.Options getBitmapOptions(String var0) {
        BitmapFactory.Options var1 = new BitmapFactory.Options();
        var1.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(var0, var1);
        return var1;
    }

    public static int calculateInSampleSize(BitmapFactory.Options var0, int var1, int var2) {
        int var3 = var0.outHeight;
        int var4 = var0.outWidth;
        int var5 = 1;
        if (var3 > var2 || var4 > var1) {
            int var6 = Math.round((float) var3 / (float) var2);
            int var7 = Math.round((float) var4 / (float) var1);
            var5 = var6 > var7 ? var6 : var7;
        }

        return var5;
    }
}
